package com.dingwei.lesyndrome.mvp.presenter;

import android.text.TextUtils;

import com.dingwei.lesyndrome.base.FailsBean;
import com.dingwei.lesyndrome.base.MyBaseBean;
import com.dingwei.lesyndrome.bean.CirclCommentListBean;
import com.dingwei.lesyndrome.bean.CircleDetailsBean;
import com.dingwei.lesyndrome.custom.myCallback;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.w3c.dom.Text;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class CircleDetailsPresenter extends basePresenter {

    private Contract.CircleDetailsView circleDetailsView;

    public CircleDetailsPresenter(Contract.CircleDetailsView view) {
        super(view);
        this.circleDetailsView = view;
    }

    public void initData(String id) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.CIRCLE_INFO);
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, CircleDetailsBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                CircleDetailsBean bean = (CircleDetailsBean) baseBean;
                circleDetailsView.setData(bean);

            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                circleDetailsView.onFail();
            }
        });
    }

    public void initCommentList(String cid, int cpage, final boolean isRefresh, boolean isShowDialog){
        RequestParams rp = new RequestParams(URLS.HOST + URLS.CIRCLE_COMMENT_LIST);
        rp.addBodyParameter("cid", cid);
        rp.addBodyParameter("cpage", cpage+"");
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, CirclCommentListBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                CirclCommentListBean bean = (CirclCommentListBean) baseBean;
                if (isRefresh){
                    circleDetailsView.onRefresh(bean);
                }else {

                }circleDetailsView.onLoadMore(bean);

            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                circleDetailsView.onFail();
            }
        });
    }


    public void like(String id) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.OPAGREE);
        rp.addBodyParameter("cid", id);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                circleDetailsView.likeSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    public void comment(String id, String content) {
        if (TextUtils.isEmpty(content)) {
            showToast("请输入内容！");
            return;
        }
        RequestParams rp = new RequestParams(URLS.HOST+URLS.COMMENT_CIRCLE);
        rp.addBodyParameter("cid", id);
        rp.addBodyParameter("content", content);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                circleDetailsView.CommentSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    public void collect(String id) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.OPCOLLECT);
        rp.addBodyParameter("oid", id);
        rp.addBodyParameter("type", "3");
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                circleDetailsView.collectSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }
}
